package com.neo.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neo.config.quartz.TaskInfo;
import com.neo.config.quartz.TaskServiceImpl;


/**
 * 任务管理
 * @author lance
 */
@Controller
@RequestMapping("/quartz")
public class TaskManageController {
	@Autowired
	private TaskServiceImpl taskServiceImpl;

	/**
	 * Index.jsp
	 * 
	 */
	@RequestMapping(value={"", "/", "index"})
	public String info(){
		return "index.jsp";
	}
	
	/**
	 * 任务列表
	 * @return
	 * 
	 */
	/*@ResponseBody
	@RequestMapping(value="list", method=RequestMethod.POST)
	public String list(){
		Map<String, Object> map = new HashMap<>();
		List<TaskInfo> infos = taskServiceImpl.list();
		map.put("rows", infos);
		map.put("total", infos.size());
		return JSON.toJSONString(map);
	}*/
	
	/**
	 * 保存定时任务
	 * @param info
	 * 
	 */
	//@ResponseBody
	@RequestMapping(value="save" )
	public String save(){
		TaskInfo taskInfo = new TaskInfo();
		taskInfo.setJobName("com.neo.config.quartz.ScheduledJob");
		taskInfo.setJobGroup("22");
		taskInfo.setCronExpression("0/2 * * * * ?");
		taskInfo.setJobDescription("这是一个任务的描述");
		try {
			if(taskInfo.getId() == 0) {
				taskServiceImpl.addJob(taskInfo);
			}else{
				taskServiceImpl.edit(taskInfo);
			}
		} catch (ServiceException e) {
			return "0";
		}
		return "1";
	}
	
	/**
	 * 删除定时任务
	 * @param jobName
	 * @param jobGroup
	 *
	 */
	/*@ResponseBody
	@RequestMapping(value="delete/{jobName}/{jobGroup}", produces = "application/json; charset=UTF-8")
	public String delete(@PathVariable String jobName, @PathVariable String jobGroup){
		try {
			taskServiceImpl.delete(jobName, jobGroup);
		} catch (ServiceException e) {
			return ResultInfo.error(-1, e.getMessage());
		}
		return ResultInfo.success();
	}*/
	
	/**
	 * 暂停定时任务
	 * @param jobName
	 * @param jobGroup
	 * 
	 */
	/*@ResponseBody
	@RequestMapping(value="pause/{jobName}/{jobGroup}", produces = "application/json; charset=UTF-8")
	public String pause(@PathVariable String jobName, @PathVariable String jobGroup){
		try {
			taskServiceImpl.pause(jobName, jobGroup);
		} catch (ServiceException e) {
			return ResultInfo.error(-1, e.getMessage());
		}
		return ResultInfo.success();
	}*/
	
	/**
	 * 重新开始定时任务
	 * @param jobName
	 * @param jobGroup
	 * 
	 */
	/*@ResponseBody
	@RequestMapping(value="resume/{jobName}/{jobGroup}", produces = "application/json; charset=UTF-8")
	public String resume(@PathVariable String jobName, @PathVariable String jobGroup){
		try {
			taskServiceImpl.resume(jobName, jobGroup);
		} catch (ServiceException e) {
			return ResultInfo.error(-1, e.getMessage());
		}
		return ResultInfo.success();
	}*/
}
